package com.invite.me.beans.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.invite.me.api.UrlConstants;
import com.invite.me.beans.fabean.HomeDlwzFaBean;
import com.invite.me.beans.fabean.HomeFaGrdllistBean;
import com.invite.me.beans.fabean.HometouxiangfaBean;
import com.invite.me.beans.res.HomeTouxiangShouBean;
import com.invite.me.beans.res.NameAtteastationtpBean;
import com.invite.me.ui.home.HomeActivity;
import com.invite.me.util.SspPrefUtils;
import com.invite.me.wlqq.Constants;
import com.invite.me.wlqq.Contract;
import com.invite.me.wlqq.RxJavaDataImpl;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class HomePresenter implements Contract.BasePresenter {
    HomeActivity homeActivity;

    public HomePresenter(HomeActivity signInActivity) {
        this.homeActivity = signInActivity;
        homeActivity.setPresenter(this);
    }

    @Override
    public void start() {

    }


    // 更新个人地里位置
    public void loadHomeData(String address,String area,String city,Double latitude,Double longitude,String province,String street) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        HomeFaGrdllistBean homeFaGrdllistBean =new HomeFaGrdllistBean();
        homeFaGrdllistBean.setAddress(address);
        homeFaGrdllistBean.setArea(area);
        homeFaGrdllistBean.setCity(city);
        homeFaGrdllistBean.setLatitude(latitude);
        homeFaGrdllistBean.setLongitude(longitude);
        homeFaGrdllistBean.setProvince(province);
        homeFaGrdllistBean.setStreet(street);

        Gson gson = new Gson();
        String phoneNumber = gson.toJson(homeFaGrdllistBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);

        RxJavaDataImpl.postData(UrlConstants.HQDQYHDWXX, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();



                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e + "");
                homeActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                homeActivity.onComplete();
            }
        });
    }


    //图片转换
    public void shangchuanweijian(File file){
        Map<String,Object> headrMap = new HashMap<>();
        //     headrMap.put("Content-Type","multipart/form-data");
        headrMap.put("userId",SspPrefUtils.getString(Constants.SSPTOKEN, ""));

        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"),file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(),requestFile);
        MultipartBody.Part namebody = MultipartBody.Part.createFormData("type","1");

        RxJavaDataImpl.uploadFiles(UrlConstants.API_SCTP,headrMap,body,namebody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {

                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        NameAtteastationtpBean gankInfo = gson.fromJson(str, NameAtteastationtpBean.class);
                        homeActivity.onDataSucess(gankInfo);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


            @Override
            public void onError(Throwable e) {

                homeActivity.onDataFaile(e.getMessage());
            }

            @Override
            public void onComplete() {
                homeActivity.onComplete();
            }
        });


    }

    //更新头像
    public void GengXingTouxiang(String touxiang) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(Constants.HEADER_KEY, Constants.HEADER_VALUE);
        hashMap.put("userId", SspPrefUtils.getString(Constants.SSPTOKEN, ""));
        HometouxiangfaBean logeInDataBean = new HometouxiangfaBean();

        logeInDataBean.setHeaderImgPath(touxiang);



        Gson gson = new Gson();
        String phoneNumber = gson.toJson(logeInDataBean);
        RequestBody requestBody = RxJavaDataImpl.getRequestBody(phoneNumber);

        RxJavaDataImpl.postData(UrlConstants.FATOUXING, hashMap, requestBody, new Observer() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(Object value) {
                if (value instanceof ResponseBody) {
                    try {
                        ResponseBody responseBody = (ResponseBody) value;
                        String str = responseBody.string();
                        Gson gson = new Gson();
                        HomeTouxiangShouBean gankInfo = gson.fromJson(str, HomeTouxiangShouBean.class);
                        homeActivity.onDataSucess(gankInfo);


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                Log.e("onerror", e + "");
                homeActivity.onDataFaile(e);
            }

            @Override
            public void onComplete() {
                homeActivity.onComplete();
            }
        });
    }

}
